﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace November {
    class NovPresenter : INovPresenter {
        private INovView View { get; set; }
        private INovModel Model { get; set; }

        public NovPresenter(INovView view, INovModel model) {
            View = view;
            Model = model;

            UpdateView();
        }

        public void OnMouseInput(MouseButtons button, Point point, Size size) {
            switch (button) {
                case MouseButtons.Left:
                    if (point.X * 2 >= size.Width) ViewNextImage();
                    else ViewPreviousImage();
                    break;
                case MouseButtons.Right:
                    ToggleFitToWidth();
                    break;
            }
        }

        public void OnKeyInput(Keys key, Boolean shift, Boolean ctrl, Boolean alt) {
            switch (key) {
                case Keys.Home:
                    ViewFirstImage();
                    break;
                case Keys.Left:
                    ViewPreviousImage();
                    break;
                case Keys.Right:
                    ViewNextImage();
                    break;
                case Keys.PageUp:
                    ViewEarlierImage();
                    break;
                case Keys.PageDown:
                    ViewLaterImage();
                    break;
                case Keys.End:
                    ViewLastImage();
                    break;
                case Keys.Escape:
                    OpenNewImage();
                    break;
                case Keys.D:
                    if (ctrl) SortByDate();
                    break;
                case Keys.N:
                    if (ctrl) SortByName();
                    break;
                case Keys.R:
                    if (ctrl) SortByRandom();
                    break;
            }
        }

        private void UpdateView() {
            Image image = Model.GetImage();
            if (image == null) {
                View.SetImage(null);
                View.SetTitle("Press ESC to Open an Image");
            }
            else {
                String name = Model.GetImageName();
                Int32 index = Model.GetImageIndex();
                Int32 count = Model.GetImageCount();

                View.SetTitle(String.Format("{0} - ( {1} / {2} )", name, index, count));
                View.SetImage(image);
            }
        }

        private void OpenNewImage() {
            String imageFilter = "*.bmp;*.gif;*.jpg;*.jpeg;*.png";
            String archiveFilter = "*.zip";

            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = String.Format("Image Files ({0})|{0}|Archive Files ({1})|{1}", imageFilter, archiveFilter);

            if (dialog.ShowDialog() == DialogResult.OK) {
                String fileName = dialog.FileName;

                if (Utility.ImageFilter(fileName)) {
                    String directoryName = Directory.GetParent(fileName).FullName;
                    Model = new DirectoryModel(directoryName, fileName);
                }
                else {
                    Model = new ArchiveModel(fileName);
                }

                UpdateView();
            }
        }

        private void ToggleFitToWidth() {
            if (View.GetViewMode() is FitToWidthViewMode) {
                View.SetViewMode(new FitToScreenViewMode());
            }
            else {
                View.SetViewMode(new FitToWidthViewMode());
            }
        }

        private void ToggleFullResolution() {
            if (View.GetViewMode() is FullResolutionViewMode) {
                View.SetViewMode(new FitToScreenViewMode());
            }
            else {
                View.SetViewMode(new FullResolutionViewMode());
            }
        }

        private void ViewFirstImage() {
            if (Model.FirstImage()) UpdateView();
        }

        private void ViewEarlierImage() {
            if (Model.EarlierImage()) UpdateView();
        }

        private void ViewPreviousImage() {
            if (Model.PreviousImage()) UpdateView();
        }

        private void ViewNextImage() {
            if (Model.NextImage()) UpdateView();
        }

        private void ViewLaterImage() {
            if (Model.LaterImage()) UpdateView();
        }

        private void ViewLastImage() {
            if (Model.LastImage()) UpdateView();
        }

        private void SortByName() {
            if (Model.SortBy(SortMode.Name)) UpdateView();
        }

        private void SortByDate() {
            if (Model.SortBy(SortMode.Date)) UpdateView();
        }

        private void SortByRandom() {
            if (Model.SortBy(SortMode.Random)) UpdateView();
        }
    }
}
